package com.ruoyi.datam.controller;

import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.UserConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.datam.service.IWiFarmerBService;
import com.ruoyi.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.domain.base.datam.WiFarmerB;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 农户信息登记Controller
 * 
 * @author hwd
 * @date 2023-02-16
 */
@RestController
@RequestMapping("/WiFarmerB")
public class WiFarmerBController extends BaseController
{
    @Autowired
    private IWiFarmerBService wiFarmerBService;


    /**
     * 根据农户名称查找农户编码
     */
    @GetMapping("/getFarmCd")
    public AjaxResult getFarmCd(String farmNm){
        String FarmCd = wiFarmerBService.getFarmCd(farmNm);
        HashMap<String, String> res = new HashMap<>();
        res.put("farmCd",FarmCd);
        return AjaxResult.success(res);
    }

    /**
     * 查询农户信息登记列表
     */
    @RequiresPermissions("datam:WiFarmerB:list")
    @GetMapping("/list")
    public TableDataInfo list(WiFarmerB wiFarmerB)
    {
        startPage();
        List<WiFarmerB> list = wiFarmerBService.selectWiFarmerBList(wiFarmerB);
        return getDataTable(list);
    }

    /**
     * 导出农户信息登记列表
     */
    @RequiresPermissions("datam:WiFarmerB:export")
    @Log(title = "农户信息登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WiFarmerB wiFarmerB)
    {
        List<WiFarmerB> list = wiFarmerBService.selectWiFarmerBList(wiFarmerB);
        ExcelUtil<WiFarmerB> util = new ExcelUtil<WiFarmerB>(WiFarmerB.class);
        util.exportExcel(response, list, "农户信息登记数据");
    }

    /**
     * 获取农户信息登记详细信息
     */
    @RequiresPermissions("datam:WiFarmerB:query")
    @GetMapping(value = "/{farmCd}")
    public AjaxResult getInfo(@PathVariable("farmCd") String farmCd)
    {
        return success(wiFarmerBService.selectWiFarmerBByFarmCd(farmCd));
    }

    /**
     * 新增农户信息登记
     */
    @RequiresPermissions("datam:WiFarmerB:add")
    @Log(title = "农户信息登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WiFarmerB wiFarmerB)
    {
        String s = wiFarmerBService.getFarmCd(wiFarmerB.getFarmNm());
        if(StringUtils.isNotBlank(s)){
            AjaxResult.error("该用户名称已存在~");
        }
        if(StringUtils.isNotBlank(wiFarmerB.getMobileNo())){
            AjaxResult.error("请认真填写手机号~");
        }
       int i= wiFarmerBService.insertWiFarmerB(wiFarmerB);
        if (i > 0) {//向user中添加數據 角色為水管員
        SysUser sysUser = new SysUser();
        sysUser.setUserName(wiFarmerB.getFarmNm());
        sysUser.setPhonenumber(wiFarmerB.getMobileNo());
        sysUser.setNickName(wiFarmerB.getFarmNm());
        sysUser.setRoleId(6L);
        if (UserConstants.NOT_UNIQUE.equals(wiFarmerBService.checkWiFarmerNameUnique(sysUser)))
        {
            return error("新增用户'" + sysUser.getUserName() + "'失败，登录账号已存在");
        }
        else if (StringUtils.isNotEmpty(sysUser.getPhonenumber())
                && UserConstants.NOT_UNIQUE.equals(wiFarmerBService.checkWiFarmerPhoneUnique(sysUser)))
        {
            return error("新增用户'" + sysUser.getUserName() + "'失败，手机号码已存在");
        }
        sysUser.setCreateBy(SecurityUtils.getUsername());
        sysUser.setPassword(SecurityUtils.encryptPassword(wiFarmerB.getMobileNo()+"@fuyun"));
            wiFarmerBService.insertWiFarmerUser(sysUser);
    }
        return toAjax(i);
    }

    /**
     * 修改农户信息登记
     */
    @RequiresPermissions("datam:WiFarmerB:edit")
    @Log(title = "农户信息登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WiFarmerB wiFarmerB)
    {
        return toAjax(wiFarmerBService.updateWiFarmerB(wiFarmerB));
    }

    /**
     * 删除农户信息登记
     */
    @RequiresPermissions("datam:WiFarmerB:remove")
    @Log(title = "农户信息登记", businessType = BusinessType.DELETE)
	@DeleteMapping("/{farmCds}")
    public AjaxResult remove(@PathVariable String[] farmCds)
    {
        return toAjax(wiFarmerBService.deleteWiFarmerBByFarmCds(farmCds));
    }
}
